import Vue from 'vue'
import Vuex from 'vuex'
import Setting from './modules/setting.js'

Vue.use(Vuex)

const store = new Vuex.Store({
	modules: {},
	state: {
		chapters: [],
		readHistory: [],
		searchKey: [],
		curSource: null,
		setting: null,
		sources: [{
				value: 'xbiquge',
				label: '新笔趣阁',
				majorFind: '#newscontent .l',
			},
			{
				value: 'zhuishushenqi',
				label: '追书神器',
				typeData: [{
						name: '热门',
						value: 'hot'
					},
					{
						name: '新书',
						value: 'new'
					},
					{
						name: '好评',
						value: 'reputation'
					},
					{
						name: '完结',
						value: 'over'
					},
					{
						name: '包月',
						value: 'monthly'
					},
				]
			},
		],
	},
	mutations: {
		// 全部章节
		set_chapters(state, chapters) {
			state.chapters = chapters
		},
		// 阅读记录
		set_readHistory(state, readHistory) {
			state.readHistory = readHistory
			uni.setStorageSync(`_readHistory_${state.curSource.value}`, readHistory)
		},
		// 阅读记录
		set_searchKey(state, searchKey) {
			state.searchKey = searchKey
			uni.setStorageSync(`_searchKey`, searchKey)
		},
		// 所有书源
		set_sources(state, sources) {
			state.sources = sources
			uni.setStorageSync('_sources', sources)
		},
		// 当前书源
		set_curSource(state, curSource) {
			state.curSource = curSource
			state.readHistory = uni.getStorageSync(`_readHistory_${curSource.value}`)
			uni.setStorageSync('_curSource', curSource)
		},
		// 当前设置
		set_setting(state, setting) {
			state.setting = setting
			uni.setStorageSync('_setting', setting)
		}
	},
	getters: {
		get_readHistory(state) {
			if (state.readHistory && state.readHistory.length > 0) {
				return state.readHistory
			}
			return uni.getStorageSync(`_readHistory_${state.curSource.value}`) || []
		},
		get_searchKey(state) {
			if (state.searchKey && state.searchKey.length > 0) {
				return state.searchKey
			}
			return uni.getStorageSync(`_searchKey`) || []
		},
		get_sources(state) {
			if (state.sources && state.sources.length > 0) {
				return state.sources
			}
			return uni.getStorageSync('_sources')
		},
		get_curSource(state) {
			if (state.curSource) {
				return state.curSource
			}
			return uni.getStorageSync('_curSource')
		},
		get_setting(state) {
			if (state.setting) {
				return state.setting
			}
			return uni.getStorageSync('_setting')
		}
	},
	actions: {
		updateReadHistory({
			state,
			commit
		}, params) {
			let t_readHistory = state.readHistory || []
			// 增加/修改
			let t_index = t_readHistory.findIndex(item => item._id === params._id)
			if (t_index !== -1) {
				let t_data = t_readHistory.splice(t_index, 1)
				params = {
					...t_data,
					...params
				}
			}
			t_readHistory.unshift(params)
			commit('set_readHistory', t_readHistory)
		},
		// 获取所有分类
		async getCategoryData({
			state
		}, params) {
			try {
				let res = ''
				switch (state.curSource.value) {
					case 'zhuishushenqi':
						res = await Vue.prototype.$minApi.getCategoryData_zssq(params)
						break
					case 'xbiquge':
						res = await Vue.prototype.$minApi.getCategoryData_xbqg(params)
						break
				}
				return res
			} catch (e) {
				console.log('store - getCategoryData', e);
				return false
			}
		},
		// 获取分类下小类别
		async getCategoryLevel({
			state
		}, params) {
			try {
				let res = ''
				switch (state.curSource.value) {
					case 'zhuishushenqi':
						res = await Vue.prototype.$minApi.getCategoryLevel_zssq(params)
						break
						// case 'xbiquge':
						// 	res = await Vue.prototype.$minApi.getCategoryLevel_xbqg(params)
						// 	break
				}
				return res
			} catch (e) {
				console.log('store - getCategoryLevel', e);
				return false
			}
		},
		// 解析页面获取书籍数据
		async getBookList({
			state
		}, params) {
			try {
				let res = ''
				switch (state.curSource.value) {
					case 'zhuishushenqi':
						res = await Vue.prototype.$minApi.getBookList_zssq(params)
						break
					case 'xbiquge':
						res = await Vue.prototype.$minApi.getBookList_xbqg(params)
						break
				}
				return res
			} catch (e) {
				console.log('store - getBookList', e);
				return false
			}
		},
		// 获取小说信息
		async getBookDetail({
			state
		}, params) {
			try {
				let res = ''
				// // 先尝试从缓存获取信息
				// res = await Vue.prototype.$fileReader(`_bd_${state.curSource.value}_${params}`)
				// if (res) {
				// 	return JSON.parse(res)
				// }

				switch (state.curSource.value) {
					case 'zhuishushenqi':
						res = await Vue.prototype.$minApi.getBookDetail_zssq(params)
						break
					case 'xbiquge':
						res = await Vue.prototype.$minApi.getBookDetail_xbqg(params)
						break
				}
				return res
			} catch (e) {
				console.log('store - getBookDetail', e);
				return false
			}
		},
		// 获取章节列表
		async getChapterList({
			state,
			commit
		}, params) {
			try {
				let res = ''
				switch (state.curSource.value) {
					case 'zhuishushenqi':
						res = await Vue.prototype.$minApi.getChapterList_zssq(params)
						break
					case 'xbiquge':
						res = await Vue.prototype.$minApi.getChapterList_xbqg(params)
						break
				}
				commit('set_chapters', res)
				return res
			} catch (e) {
				console.log('store - getChapterList', e);
				return false
			}
		},
		// 搜索
		async getBookByKey({
			state
		}, params) {
			try {
				let res = ''
				switch (state.curSource.value) {
					case 'zhuishushenqi':
						params = {
							query: params
						}
						res = await Vue.prototype.$minApi.getBookByKey_zssq(params)
						break
					case 'xbiquge':
						params = {
							searchkey: params
						}
						res = await Vue.prototype.$minApi.getBookByKey_xbqg(params)
						break
				}
				return res
			} catch (e) {
				console.log('store - getBookByKey', e);
				return false
			}
		},
		// 获取章节内容
		async getBookContent({
			state
		}, params) {
			try {
				let res = ''
				// 先尝试从缓存获取信息
				// chapterContent
				res = await Vue.prototype.$fileReader(
					`_cc_${state.curSource.value}_${params.id}_${params.index}`)
				if (res) {
					return JSON.parse(res)
				}

				switch (state.curSource.value) {
					case 'zhuishushenqi':
						res = await Vue.prototype.$minApi.getBookContent_zssq(params)
						break
					case 'xbiquge':
						res = await Vue.prototype.$minApi.getBookContent_xbqg(params)
						break
				}

				if (res) {
					// bookChapterCache
					let bcc = Vue.prototype.$fileReader(`_bcc_${state.curSource.value}_${params.id}`)
					if (bcc) {
						bcc = [params.index]
					} else {
						bcc.indexOf(params.index) > -1 ? '' : bcc.push(params.index)
					}
					Vue.prototype.$fileWriter(`_bcc_${state.curSource.value}_${params.id}`,
						JSON.stringify(bcc))
					// chapterContent
					Vue.prototype.$fileWriter(
						`_cc_${state.curSource.value}_${params.id}_${params.index}`,
						JSON.stringify(res))
				}
				return res
			} catch (e) {
				console.log('store - getBookContent', e);
				return false
			}
		},
	}
})

export default store
